/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* SwingUtil.java
*
* Created on November 6, 2007, 7:09 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package javautil.swing;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;

/**
 * This has utility class regarding swing package
 * @author Susanta Tewari
 * @version 1.0
 * @updated 06-Nov-2007 11:45:05 PM
 */
public class SwingUtil {

    /** Creates a new instance of SwingUtil */
    public SwingUtil() {}

    public static boolean isYesInYesNoOptionDialog(Component parent, String msg) {

        int val = 0;

        try {

            val = JOptionPane.showConfirmDialog(parent, msg, "Information !",
                    JOptionPane.YES_NO_OPTION);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return val == JOptionPane.YES_OPTION;
    }

    public static boolean isYesInExitDialog(Component parent) {

        int val = 0;

        try {

            val = JOptionPane.showConfirmDialog(parent, "Do you want to close the application ?",
                    "Exit ?", JOptionPane.YES_NO_OPTION);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return val == JOptionPane.YES_OPTION;
    }

    public static File getFileFromSaveAs(Component parent, FileFilter fileFilter) {

        JFileChooser chooser = new JFileChooser();

        chooser.addChoosableFileFilter(fileFilter);

        int returnVal = chooser.showSaveDialog(parent);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            return chooser.getSelectedFile();
        }

        return null;
    }

    public static void showErrorDialog(Component parent, String message) {
        JOptionPane.showMessageDialog(parent, message, "ERROR:", JOptionPane.ERROR_MESSAGE);
    }

    public static void showMessageDialog(Component parent, String message) {
        JOptionPane.showMessageDialog(parent, message, "NOTE:", JOptionPane.INFORMATION_MESSAGE);
    }

    public static int getScreenWidth() {
        return Toolkit.getDefaultToolkit().getScreenSize().width;
    }

    public static void showFrame(Component comp) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                 | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JFrame frame         = new JFrame();
        JPanel panel         = new JPanel();

        panel.add(comp);

        Dimension frameSize = frame.getSize();

        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }

        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }

        frame.setLocation((screenSize.width - frameSize.width) / 2,
                          (screenSize.height - frameSize.height) / 2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }
}
